#ifndef MESSAGEHEADER_H
#define MESSAGEHEADER_H

#include <string>
#include <cstdio>
#include <map>
using std::string;

class MessageHeader {
public:
    // Add a new name-value pair
    void appendField(const string& fieldName, const string& fieldValue);
    // Return true if fieldName is set.
    bool isFieldExists(const string& fieldName) const;
    // Return the value of consisting fieldName, or empty string if
    // fieldName has not been set
    string getFieldValue(const string& fieldName) const;
    // print the current header in the HTTP style
    void print(FILE* file) const;
    void printfd(int fd) const;
    // parse a HTTP format field name-value string, and append it.
    // return false and nothing changes if parsing is failed.
    bool parseAppend(const string& str);
    // parse a HTTP format headers from file. append them.
    // return false if anything goes wrong. In that moment,
    // it's unstable and you need to call clear()
    bool parseHeader(int fd);
    // Delete all name-value pairs
    void clear();
private:
    typedef std::map<string, string> NameValueMap;
    NameValueMap m_fields;
};

#endif
